package org.linlinjava.litemall.core.util;


import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Collections;


public class GoogleUtil {

    public static Payload getPayload(String idTokenString) throws GeneralSecurityException, IOException {
//        System.out.println(idTokenString.indexOf(4));
        GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new NetHttpTransport(),
                JacksonFactory.getDefaultInstance())
                // Specify the CLIENT_ID of the app that accesses the backend:
                // Or, if multiple clients access the backend:
                .setAudience(Arrays.asList( "609863388165-m9p2aenin2gka9noqi1p81fvrvl7i4nc.apps.googleusercontent.com"
                        ,"609863388165-hnpdin78ee2qt4582tarsvgv2eh52r1j.apps.googleusercontent.com"
                        ,"609863388165-23j1cae02behsgb2nomd5cragt3ugnvc.apps.googleusercontent.com"
                        ,"609863388165-1ofpt3f4dc1gasi8jbdkv8fmogej0hdf.apps.googleusercontent.com"
                        ,"609863388165-ockhsc7b498nnh53gn5unp3oc5bj021i.apps.googleusercontent.com"))
                .build();

        GoogleIdToken idToken = verifier.verify(idTokenString);
        Payload payload = null;
        if (idToken != null) {
            payload = idToken.getPayload();

            // Print user identifier
            String userId = payload.getSubject();

            // Get profile information from payload
//            String email = payload.getEmail();
//            boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
//            String name = (String) payload.get("name");
//            String pictureUrl = (String) payload.get("picture");
//            String locale = (String) payload.get("locale");
//            String familyName = (String) payload.get("family_name");
//            String givenName = (String) payload.get("given_name");

        }
        return  payload;
    }
}

